﻿using System.ComponentModel;
using org.se.fs.presenter.Library;

namespace org.se.fs.presenter.Task.Template
{
    public abstract class Crud<T> where T : new()
    {
        private BackgroundWorker _bgwFetch;
        private BackgroundWorker _bgwFetchLists;
        private BackgroundWorker _bgwInsert;
        private BackgroundWorker _bgwUpdate;
        private BackgroundWorker _bgwDelete;
        private BackgroundWorker _bgwToggle;
        public event FormEvent.FormEventHandler OnFetch;
        public event FormEvent.FormEventHandler OnFetchLists;
        public event FormEvent.FormEventHandler OnInsert;
        public event FormEvent.FormEventHandler OnUpdate;
        public event FormEvent.FormEventHandler OnDelete;
        public event FormEvent.FormEventHandler OnToggle;
        protected bool IsSuccess;

        protected void TriggerFetch(object source, FormEvent e)
        {
            if (OnFetch != null)
                OnFetch(source, e);
        }

        protected void TriggerFetchLists(object source, FormEvent e)
        {
            if (OnFetchLists != null)
                OnFetchLists(source, e);
        }

        protected void TriggerInsert(object source, FormEvent e)
        {
            if (OnInsert != null)
                OnInsert(source, e);
        }

        protected void TriggerUpdate(object source, FormEvent e)
        {
            if (OnUpdate != null)
                OnUpdate(source, e);
        }

        protected void TriggerDelete(object source, FormEvent e)
        {
            if (OnDelete != null)
                OnDelete(source, e);
        }

        protected void TriggerToggle(object source, FormEvent e)
        {
            if (OnToggle != null)
                OnToggle(source, e);
        }

        protected virtual void BgwFetchDoWork(object sender, DoWorkEventArgs e) { }

        protected virtual void BgFetchRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { }

        protected virtual void BgwFetchListsDoWork(object sender, DoWorkEventArgs e) { }

        protected virtual void BgFetchListsRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { }

        protected virtual void BgwInsertDoWork(object sender, DoWorkEventArgs e) { }

        protected virtual void BgInsertRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { }

        protected virtual void BgwUpdateDoWork(object sender, DoWorkEventArgs e) { }

        protected virtual void BgUpdateRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { }

        protected virtual void BgwDeleteDoWork(object sender, DoWorkEventArgs e) { }

        protected virtual void BgDeleteRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { }

        protected virtual void BgwToggleDoWork(object sender, DoWorkEventArgs e) { }

        protected virtual void BgToggleRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { }

        public virtual void Fetch(T data)
        {
            _bgwFetch = new BackgroundWorker();
            _bgwFetch.DoWork += BgwFetchDoWork;
            _bgwFetch.RunWorkerCompleted += BgFetchRunWorkerCompleted;
            _bgwFetch.RunWorkerAsync(data);
        }

        public virtual void FetchLists(T data)
        {
            _bgwFetchLists = new BackgroundWorker();
            _bgwFetchLists.DoWork += BgwFetchListsDoWork;
            _bgwFetchLists.RunWorkerCompleted += BgFetchListsRunWorkerCompleted;
            _bgwFetchLists.RunWorkerAsync(data);
        }

        public virtual void Insert(T data)
        {
            _bgwInsert = new BackgroundWorker();
            _bgwInsert.DoWork += BgwInsertDoWork;
            _bgwInsert.RunWorkerCompleted += BgInsertRunWorkerCompleted;
            _bgwInsert.RunWorkerAsync(data);
        }

        public virtual void Update(T data)
        {
            _bgwUpdate = new BackgroundWorker();
            _bgwUpdate.DoWork += BgwUpdateDoWork;
            _bgwUpdate.RunWorkerCompleted += BgUpdateRunWorkerCompleted;
            _bgwUpdate.RunWorkerAsync(data);
        }

        public virtual void Delete(T data)
        {
            _bgwDelete = new BackgroundWorker();
            _bgwDelete.DoWork += BgwDeleteDoWork;
            _bgwDelete.RunWorkerCompleted += BgDeleteRunWorkerCompleted;
            _bgwDelete.RunWorkerAsync(data);
        }

        public virtual void Toggle(T data)
        {
            _bgwToggle = new BackgroundWorker();
            _bgwToggle.DoWork += BgwToggleDoWork;
            _bgwToggle.RunWorkerCompleted += BgToggleRunWorkerCompleted;
            _bgwToggle.RunWorkerAsync(data);
        }
    }
}